import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/auth/UserIdentity.dart';
import 'package:bxultimate/domain/auth/login_token.dart';
import 'package:bxultimate/domain/auth/login_user.dart';
import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/domain/user/ElectronIdCardDto.dart';
import 'package:bxultimate/domain/user/OrderDto.dart';
import 'package:bxultimate/domain/user/ThreeLoginDto.dart';
import 'package:bxultimate/domain/user/UserInformation.dart';
import 'package:bxultimate/shared/shared.dart';
import 'package:dio/dio.dart';


/// 用户
class UserRequest {
  /// 通过手机号密码登录
  static Future<LoginToken> loginByPassword(
      mobile, password) async {
    var response = await UtilHttp().post('/uaa/oauth/token',
        data: {
          "grant_type": "mobile_password",
          "mobile": mobile,
          "password": password,
        },
        options: Options(headers: {
          "Authorization": "Basic Qlg6d2ViQXBw",
          "Content-Type": "application/x-www-form-urlencoded"
        }));
    return LoginToken.fromJson(response['data']);
  }

  static Future<LoginToken> loginByMobile(mobile, smsCode) async {
    var response = await UtilHttp().post('/uaa/oauth/token',
        data: {
          "grant_type": "mobile_code",
          "mobile": mobile,
          "smsCode": smsCode,
        },
        options: Options(headers: {
          "Authorization": "Basic Qlg6d2ViQXBw",
          "content-type": "application/x-www-form-urlencoded"
        }));
    return LoginToken.fromJson(response['data']);
  }

  /// 获取用户信息
  static Future<LoginUser> profile() async {
    var response = await UtilHttp().get(
      '/user/users/current',
    );
    return LoginUser.fromJson(response['data']);
  }

  // 发送登录验证码
  static Future<Result> sendLoginSmsCode(mobile) async {
    var response = await UtilHttp().get(
      '/uaa/validata/smsCode/' + mobile,
    );
    return Result.fromJson(response);
  }

  // 发送注册验证码
  static Future<Result> sendRegisterSmsCode(mobile) async {
    var response = await UtilHttp().get(
      '/user/sendRegisterCode/' + mobile,
    );
    return Result.fromJson(response);
  }

  // 发送找回密码验证码
  static Future sendForgotPwdCode(mobile) async {
    var response = await UtilHttp().get(
      '/user/sendForgotPwdCode/' + mobile,
    );
    return response;
  }

  // 注册
  static Future<Result> register(username, mobile, smsCode, password) async {
    var response = await UtilHttp().post('/user/users/register', data: {
      "username": username,
      "mobile": mobile,
      "smsCode": smsCode,
      "password": password,
      "type": "APP"
    });
    return Result.fromJson(response);
  }

  // 找回密码
  static Future<Result> forgotPwd( mobile, smsCode, password) async {
    var response = await UtilHttp().post('/user/upPwdByMobile', data: {
      "mobile": mobile,
      "smsCode": smsCode,
      "password": password,
    });
    return Result.fromJson(response);
  }
  // 修改密码
  static Future<Result> upPwdByPwd( mobile, password, rePassword) async {
    var response = await UtilHttp().post('/user/upPwdByPwd', data: {
      "mobile": mobile,
      "password": password,
      "newPassword": rePassword,
    });
    return Result.fromJson(response);
  }
  // 修改用户信息
 static Future updateUser( nickname) async {
    var response = await UtilHttp().put('/user/users', data: {
      "id":AuthStore.to.profile.id,
      "nickname": nickname,
    });
    return response;
  }

  // 退出登录
  static Future logout() async {
    return await UtilHttp().post(
      '/user/logout',
    );
  }

  // 用户协议
  static Future<String> getProtocol() async {
    var response = await UtilHttp().get(
      '/app/appProtocol/selectAllByCode/yhxy',
    );
    return response['content'];
  }

  // 隐私政策
  static Future<String> getPriviacyPolicy() async {
    var response = await UtilHttp().get(
      '/app/appProtocol/selectAllByCode/yszc',
    );
    return response['content'];
  }



  // 隐私政策
  static Future<UserIdentity> selectUserIdentityInfoByIdcard() async {
    var response = await UtilHttp().get(
      '/app/userIdentity/selectUserIdentityInfoByIdcard',
    );
    return UserIdentity.fromJson(response['data']);
  }


  // 获取个人资料
  static Future<UserInformation> getUserInformation() async {
    var response = await UtilHttp().get(
      '/app/appUser/queryByIdCard',
    );
    return UserInformation.fromJson(response['data']);
  }
  // 完善资料
  static Future<Result> perfectManyInfo(data) async {
    var response = await UtilHttp().post(
      '/app/appUser/perfectManyInfo',
      data: data
    );
    return Result.fromJson(response);
  }

  // 更新头像或昵称
  static Future<Result> updateNickNameOrAvatar(data) async {
    var response = await UtilHttp().post(
      '/app/andriod/user/updateUserNickNameAndHeadImgBySysUserId',
      data: data
    );
    return Result.fromJson(response);
  }

  // 获取我的订单列表
  static Future<List<OrderDto>> getMyOrderList(page,size) async {
    var response = await UtilHttp().get(
      '/app/andriod/shopOrder/getUserShopOrderList?pageNumber=${page}&pageSize=${size}',
    );
    return response['data']['records']
        .map<OrderDto>((item) => OrderDto.fromJson(item))
        .toList();
  }
  // 意见反馈
  static Future<Result> userFeedback(data) async {
    var response = await UtilHttp().post(
      '/app/userFeedback',
      data:data
    );
    return Result.fromJson(response);
  }

   // 电子身份证
  static Future<ElectronIdCardDto> generateElectronicIdcard(idcard) async {
    var response = await UtilHttp().get(
      '/app/electronicIdcard/generateElectronicIdcard?idcard=' + idcard,
    );
    return ElectronIdCardDto.fromJson(response['data']);
  }

  static Future<List<ThreeLoginDto>> getThreeLogin()async {
    var response = await UtilHttp().get(
    '/user/getThreeLogin',
    );
    return response['data']
        .map<ThreeLoginDto>((item) => ThreeLoginDto.fromJson(item))
        .toList();
  }

//
 static Future<Result> scanConfirm(key)async {
    var response = await UtilHttp().get(
    '/uaa/scan/scanConfirm?key=' + key,
    );
    return Result.fromJson(response['data']);
  }

  static Future<Result> scanCodeUserLogin(key) async {
    var response = await UtilHttp().get(
      '/uaa/scan/scanCodeUserLogin?key=${key}&userId=${AuthStore.to.profile.id}',
    );
    return Result.fromJson(response['data']);
  }
}
